home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / invest / widgets.pyc (.txt) < prev   
Encoding:
Python Compiled Bytecode  |  2009-10-12  |  6.8 KB  |  207 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. import os
  5. import time
  6. from os.path import *
  7. import gnomeapplet
  8. import gtk
  9. import gtk.gdk as gtk
  10. import gconf
  11. import gobject
  12. import pango
  13. from gettext import gettext as _
  14. import gtk
  15. import gobject
  16. import csv
  17. import os
  18. from gettext import gettext as _
  19. import invest
  20. import invest.about as invest
  21. import invest.chart as invest
  22. from invest import *
  23. COLORSCALE_POSITIVE = [
  24.     'white',
  25.     '#ad7fa8',
  26.     '#75507b',
  27.     '#5c3566',
  28.     '#729fcf',
  29.     '#3465a4',
  30.     '#204a87',
  31.     '#8ae234',
  32.     '#73d216',
  33.     '#4e9a06']
  34. GREEN = COLORSCALE_POSITIVE[-1]
  35. COLORSCALE_NEGATIVE = [
  36.     'white',
  37.     '#fce94f',
  38.     '#e9b96e',
  39.     '#fcaf3e',
  40.     '#c17d11',
  41.     '#f57900',
  42.     '#ce5c00',
  43.     '#ef2929',
  44.     '#cc0000',
  45.     '#a40000']
  46. RED = COLORSCALE_NEGATIVE[-1]
  47. TICKER_TIMEOUT = 10000
  48.  
  49. class InvestWidget(gtk.TreeView):
  50.     
  51.     def __init__(self, quotes_updater):
  52.         gtk.TreeView.__init__(self)
  53.         self.set_property('rules-hint', True)
  54.         self.set_reorderable(True)
  55.         simple_quotes_only = quotes_updater.simple_quotes_only()
  56.         col_names = [
  57.             _('Ticker'),
  58.             _('Last'),
  59.             _('Change %'),
  60.             _('Chart'),
  61.             _('Gain'),
  62.             _('Gain %')]
  63.         col_cellgetdata_functions = [
  64.             self._getcelldata_symbol,
  65.             self._getcelldata_value,
  66.             self._getcelldata_variation,
  67.             None,
  68.             self._getcelldata_balance,
  69.             self._getcelldata_balancepct]
  70.         for i, col_name in enumerate(col_names):
  71.             if i < 3:
  72.                 cell = gtk.CellRendererText()
  73.                 column = gtk.TreeViewColumn(col_name, cell)
  74.                 column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  75.                 self.append_column(column)
  76.                 continue
  77.             if i == 3:
  78.                 cell_pb = gtk.CellRendererPixbuf()
  79.                 column = gtk.TreeViewColumn(col_name, cell_pb, pixbuf = 6)
  80.                 self.append_column(column)
  81.                 continue
  82.             if simple_quotes_only == False:
  83.                 cell = gtk.CellRendererText()
  84.                 column = gtk.TreeViewColumn(col_name, cell)
  85.                 column.set_cell_data_func(cell, col_cellgetdata_functions[i])
  86.                 self.append_column(column)
  87.                 continue
  88.         
  89.         if simple_quotes_only == True:
  90.             self.set_property('headers-visible', False)
  91.         
  92.         self.connect('row-activated', self.on_row_activated)
  93.         self.set_model(quotes_updater)
  94.  
  95.     
  96.     def _getcelldata_symbol(self, column, cell, model, iter):
  97.         cell.set_property('text', model[iter][model.SYMBOL])
  98.  
  99.     
  100.     def _getcelldata_value(self, column, cell, model, iter):
  101.         cell.set_property('text', '%.5g' % model[iter][model.VALUE])
  102.  
  103.     
  104.     def _getcelldata_variation(self, column, cell, model, iter):
  105.         color = GREEN
  106.         if model[iter][model.VARIATION_PCT] < 0:
  107.             color = RED
  108.         
  109.         change_pct = model[iter][model.VARIATION_PCT]
  110.         cell.set_property('markup', "<span foreground='%s'>%+.2f%%</span>" % (color, change_pct))
  111.  
  112.     
  113.     def _getcelldata_balance(self, column, cell, model, iter):
  114.         is_ticker_only = model[iter][model.TICKER_ONLY]
  115.         color = GREEN
  116.         if model[iter][model.BALANCE] < 0:
  117.             color = RED
  118.         
  119.         if is_ticker_only:
  120.             cell.set_property('text', '')
  121.         else:
  122.             cell.set_property('markup', "<span foreground='%s'>%+.2f</span>" % (color, model[iter][model.BALANCE]))
  123.  
  124.     
  125.     def _getcelldata_balancepct(self, column, cell, model, iter):
  126.         is_ticker_only = model[iter][model.TICKER_ONLY]
  127.         color = GREEN
  128.         if model[iter][model.BALANCE] < 0:
  129.             color = RED
  130.         
  131.         if is_ticker_only:
  132.             cell.set_property('text', '')
  133.         else:
  134.             cell.set_property('markup', "<span foreground='%s'>%+.2f%%</span>" % (color, model[iter][model.BALANCE_PCT]))
  135.  
  136.     
  137.     def on_row_activated(self, treeview, path, view_column):
  138.         ticker = self.get_model()[self.get_model().get_iter(path)][0]
  139.         if ticker == None:
  140.             return None
  141.         invest.chart.show_chart([
  142.             ticker])
  143.  
  144.  
  145.  
  146. class InvestTrend(gtk.Image):
  147.     
  148.     def __init__(self):
  149.         gtk.Image.__init__(self)
  150.         self.pixbuf = None
  151.         self.previous_allocation = (0, 0)
  152.         self.connect('size-allocate', self.on_size_allocate)
  153.         get_quotes_updater().connect('quotes-updated', self.on_quotes_update)
  154.  
  155.     
  156.     def on_size_allocate(self, widget, allocation):
  157.         if self.previous_allocation == (allocation.width, allocation.height):
  158.             return None
  159.         self.pixbuf = gtk.gdk.Pixbuf(gtk.gdk.COLORSPACE_RGB, True, 8, allocation.height, allocation.height)
  160.         self.set_color('grey')
  161.         self.previous_allocation = (allocation.width, allocation.height)
  162.  
  163.     
  164.     def set_color(self, color, opacity = 255):
  165.         if self.pixbuf != None:
  166.             
  167.             try:
  168.                 color = pango.Color(color)
  169.                 factor = float(255) / 65535
  170.                 self.pixbuf.fill(int(color.red * factor) << 24 | int(color.green * factor) << 16 | int(color.blue * factor) << 8 | opacity)
  171.                 self.set_from_pixbuf(self.pixbuf)
  172.             except Exception:
  173.                 e = None
  174.                 print e
  175.             except:
  176.                 None<EXCEPTION MATCH>Exception
  177.             
  178.  
  179.         None<EXCEPTION MATCH>Exception
  180.  
  181.     
  182.     def on_quotes_update(self, updater):
  183.         start_total = 0
  184.         now_total = 0
  185.         for row in updater:
  186.             if row[updater.TICKER_ONLY]:
  187.                 continue
  188.             
  189.             var = row[updater.VARIATION] / 100
  190.             now = row[updater.VALUE]
  191.             start = now / (1 + var)
  192.             portfolio_number = []([ purchase['amount'] for purchase in invest.STOCKS[row[updater.SYMBOL]] ])
  193.             start_total += start * portfolio_number
  194.             now_total += now * portfolio_number
  195.         
  196.         day_var = 0
  197.         color = int(2 * day_var)
  198.         opacity = min(255, int(abs(127.5 * day_var)))
  199.         if day_var < 0:
  200.             color = COLORSCALE_NEGATIVE[min(len(COLORSCALE_NEGATIVE) - 1, abs(color))]
  201.         else:
  202.             color = COLORSCALE_POSITIVE[min(len(COLORSCALE_POSITIVE) - 1, abs(color))]
  203.         self.set_color(color, opacity)
  204.  
  205.  
  206. gobject.type_register(InvestTrend)
  207.